package com.usermanage.controller;

import com.alibaba.fastjson.JSONArray;
import com.change.Service.AirSoftService;
import com.change.bean.SkuBean;
import com.contract.Bean.SkuPriceTableDetail;
import com.contract.Bean.TablePriceChgDtl;
import com.contract.Bean.TablePriceChgSmy;
import com.contract.Common.SkuPriceTableIOC;
import com.contract.Common.TablePriceChgIOC;
import com.contract.Service.TablePriceChgDtlService;
import com.contract.Service.TablePriceChgSmyService;
import com.ordercore.Util.ResponseData.ResponseData;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.MessageService;
import com.usermanage.beanParam.PermissionBean;
import com.usermanage.util.StaticUtil;
import com.usermanage.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tariffController")
public class TariffController {


    @RequestMapping("/list.page")
    public ModelAndView listPage() {
        ModelAndView mv = new ModelAndView();
        //获得用户登陆信息
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        //从用户登陆信息中获取用户权限列表
        List<PermissionBean> permissionBeanList = userDetail.getList();
        //判断用户具有的权限在界面展示
        List<PermissionBean> result = new ArrayList<>();
        for (PermissionBean bean : permissionBeanList) {
            if (bean.getPermission_url().equals("tariffAdd")) {
                result.add(bean);
            }
            if (bean.getPermission_url().equals("tariffApprove.json")) {
                mv.addObject("approve", true);
            }
            if (bean.getPermission_url().equals("tariffModify.json")) {
                mv.addObject("modify", true);
            }
            if (bean.getPermission_url().equals("tariffEdit.json")) {
                mv.addObject("edit", true);
            }
            if (bean.getPermission_url().equals("tariffCancel.json")) {
                mv.addObject("cancel", true);
            }
        }
        mv.addObject("permissionList", result);
        mv.setViewName("/tariff/list");
        return mv;
    }


    @RequestMapping(value = "/detail.page")
    public ModelAndView detailPage(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("/tariff/detail");
        return mv;
    }

    @RequestMapping(value = "/add.page")
    public ModelAndView addPage(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("/tariff/add");
        return mv;
    }

    @RequestMapping(value = "/edit.page")
    public ModelAndView editPage(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("/tariff/edit");
        return mv;
    }

    @RequestMapping(value = "/modify.page")
    public ModelAndView modifyPage(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("/tariff/modify");
        return mv;
    }

    @RequestMapping(value = "/skuPriceTableSummaryList.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData skuPriceTableSummaryList(int page, int limit, String key, String stat) {
        return SkuPriceTableIOC.skuPriceTableSummaryList(page, limit, key, stat);
    }

    @RequestMapping(value = "/skuPriceTableUpdate.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResponseData skuPriceTableUpdate(Integer id, String name, String stat, String list) {
        return SkuPriceTableIOC.skuPriceTableUpdate(id, name, stat, list);
    }

    @RequestMapping(value = "/skuPriceTableDetailList.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public static ResponseData skuPriceTableDetailList(int page, int limit, String key, Integer id, String stat) {
        return SkuPriceTableIOC.skuPriceTableDetailList(id, key, stat, page, limit);
    }

    @RequestMapping(value = "/addTablePrice.json")
    public ResponseData addTablePrice() {
        ResponseData responseData = SkuPriceTableIOC.addTablePrice();
        return responseData;
    }

    @RequestMapping(value = "/skuPriceTableSummaryDetail.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public static ResponseData skuPriceTableSummaryDetail(Integer id) {
        return SkuPriceTableIOC.skuPriceTableSummaryDetail(id);
    }

    @RequestMapping(value = "/skuPriceTableDetailUpt.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public static ResponseData skuPriceTableDetailUpt(SkuPriceTableDetail skuPriceTableDetail) {
        return SkuPriceTableIOC.skuPriceTableDetailUpt(skuPriceTableDetail);
    }

    @Transactional
    @RequestMapping(value = "/tablePriceChgSmyAdd.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResponseData tablePriceChgSmyAdd(TablePriceChgSmy tablePriceChgSmy, String obj) {
        try {
            ResponseData responseData = TablePriceChgIOC.tablePriceChgSmyAdd(tablePriceChgSmy);
            tablePriceChgSmy = (TablePriceChgSmy) responseData.getData();

            List<TablePriceChgDtl> list = JSONArray.parseArray(obj, TablePriceChgDtl.class);

            List<TablePriceChgDtl> listInsert = new ArrayList();
                for (int i = 0; i < list.size(); ++i) {
                    TablePriceChgDtl tablePriceChgDtl = list.get(i);
                    if ("00".equals(tablePriceChgDtl.getModify())) {
                        tablePriceChgDtl.setTp_dtl_id(tablePriceChgDtl.getId());
                        listInsert.add(tablePriceChgDtl);
                    }
                }
            TablePriceChgIOC.tablePriceChgAdd(list, tablePriceChgSmy.getId(), tablePriceChgSmy.getTableprice_id());
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.fail("进入异常！");
        }
    }

}
